<template>
  <div @click="showDrawer">导出</div>
  <a-drawer
    v-model:open="open"
    title="导出"
    placement="right"
    width="400"
    class="drawer"
  >
    <div style="overflow: auto; height: 100%">
      <a-tree
        class="tree"
        v-model:checkedKeys="checkedKeys"
        checkable
        defaultExpandAll
        :blockNode="true"
        :show-line="{ showLeafIcon: false }"
        :tree-data="treeData"
        :fieldNames="{ key: 'categoryId', title: 'categoryName' }"
      >
        <template #switcherIcon="{ switcherCls }">
          <down-outlined :class="switcherCls" />
        </template>
        <template #title="{ categoryName }">
          <div class="title">
            {{ categoryName }}
          </div>
        </template>
      </a-tree>
    </div>
    <template #footer>
      <div style="text-align: right">
        <a-button style="margin-right: 8px" @click="onClose">取消</a-button>
        <a-button
          type="primary"
          @click="onConfirm"
          :loading="loading"
          :disabled="checkedKeys.length === 0"
        >
          确定
        </a-button>
      </div>
    </template>
  </a-drawer>
  <a-modal
    v-model:open="openModal"
    title="导出"
    :footer="null"
    :closable="!loading"
    :maskClosable="!loading"
  >
    <div class="modal-box before-border">
      <div class="modal-content">
        <a-progress
          v-if="!loading"
          type="circle"
          :percent="100"
          :size="80"
          :status="error ? 'exception' : null"
        />
        <div class="modal-tip" v-if="loading">文件导出中...</div>
        <div class="modal-tip" v-else>
          {{ error ? '文件导出失败' : '文件导出成功' }}
        </div>
        <div style="display: flex; justify-content: center">
          <div>
            <a-button v-if="error" @click="onConfirm">重新导出</a-button>
          </div>
        </div>
      </div>
    </div>
  </a-modal>
</template>
<script setup>
import { onMounted, ref } from 'vue'
import { DownOutlined } from '@ant-design/icons-vue'
import { getCategoryTree } from '@/api/db.js'
import { exportDB } from '@/api/db.js'
import { downloadFormBlob } from '@/utils/util.js'
import { message as Message } from 'ant-design-vue'
import { useDbType } from './provide-context.js'

/**
 * 抽屉相关
 */
let open = ref(false)
let showDrawer = () => {
  open.value = true
}
function onClose() {
  open.value = false
}
let loading = ref(false)
let error = ref()
let openModal = ref(false)
let dbType = useDbType()
async function onConfirm() {
  openModal.value = true
  loading.value = true
  error.value = null
  try {
    await exportDB({ fileIds: checkedKeys.value.join(',') }, dbType).then(
      downloadFormBlob,
    )
    open.value = false
  } catch (e) {
    error.value = e
    // Message.error('导出失败')
  }
  // openModal.value = false
  loading.value = false
}
/**
 * 数据相关
 */
let treeData = ref([])
let checkedKeys = ref([])
/**
 * 数据初始化
 */
onMounted(async () => {
  try {
    let result = await getCategoryTree({}, dbType)
    treeData.value = result.data || []
  } catch (e) {}
})
</script>
<style lang="less" scoped>
.button-wrap {
  display: flex;
  align-items: center;
  color: rgba(0, 0, 0, 0.25);
}
.drawer {
  .time {
    color: rgba(0, 0, 0, 0.15);
    text-align: center;
    margin-bottom: 6px;
  }
  .ant-divider {
    margin: 20px 0;
  }
}
.modal-content {
  text-align: center;
  margin-top: 33px;
  .modal-tip {
    font-size: 22px;
    color: rgba(0, 0, 0, 0.85);
    line-height: 34px;
    font-weight: 500;
    margin: 22px 0;
  }
}
</style>
